<template>
  <div>
    <div>
      <el-input v-model="search" class="input-with-select" placeholder="请输入内容" style="width: 20%"
                @keyup.enter.native="searchClick">
        <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
      </el-input>
      <el-button
        :disabled="debuilds.length === 0"
        icon="el-icon-delete"
        style="float:left;"
        type="danger"
        @click="alldeBuild">
        一键删除
      </el-button>
    </div>
    <el-table
      v-loading="loading"
      :data="builds.records"
      border
      element-loading-background="rgba(0, 0, 0, 0.8)"
      element-loading-spinner="el-icon-loading"
      element-loading-text="拼命加载中"
      style="width: 100%"
      @selection-change="changeBox">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        align="center"
        label="Id"
        prop="id">
      </el-table-column>
      =
      <el-table-column
        align="center"
        label="楼栋ID"
        prop="buildId">
      </el-table-column>
      <el-table-column
        align="center"
        label="管理员ID"
        prop="userId">
      </el-table-column>
      <el-table-column
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="addCleandialog">添加楼栋管理员信息</el-button>
    <!--      添加页面-->
    <el-dialog
      :title="title"
      :visible.sync="addVisible"
      width="450px">
      <el-form :model="build"
               label-position="top"
               label-width="50px"
               size="min"
      >
        <el-form-item label="楼栋">
          <el-select v-model="build.buildId">
            <el-option :label="b.buildName" :value="b.buildId" v-for="b in buildIds" :key="b.buildId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户">
          <el-select v-model="build.userId">
            <el-option :label="b.username" :value="b.userId" v-for="b in userIds" :key="b.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="addVisible = false">取消</el-button>
          <el-button type="primary" @click="addFrom">{{ title }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        background
        hide-on-single-page
        layout="prev, pager, next"
        @prev-click="prevClick"
        @next-click="nextClick"
        @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import buildUser from '@/utils/buildUser'
import build from '@/utils/build'
import user from '@/utils/User'

export default {
  name: 'Build',
  data () {
    return {
      buildIds: [],
      userIds: [],
      ids: [],
      debuilds: [],
      title: '',
      builds: [],
      search: '',
      total: 0,
      addVisible: false,
      loading: true,
      limit: 10,
      build: {
        id: '',
        buildId: '',
        userId: ''
      },
      users: [],
      dialogVisible: false
    }
  },
  methods: {
    handleEdit (index, row) {
      Object.assign(this.build, row)
      this.title = '修改'
      this.addVisible = true
    },
    editSubmit () {
      buildUser.editBuildUser(this.build).then(() => {
        this.getBuilds(this.builds.current, this.limit, this.search)
        this.$message.success('修改成功!')
        this.build = {}
      })
    },
    addCleandialog () {
      this.build = {}
      this.title = '添加'
      this.addVisible = true
    },
    addFrom () {
      this.title === '添加'
        ? buildUser.addBuildUser(this.build).then(() => {
          this.getBuilds(this.builds.current, this.limit, this.search)
          this.$message.success(this.title + '成功!')
          this.addVisible = false
        })
        : buildUser.editBuildUser(this.build).then(() => {
          this.getBuilds(this.builds.current, this.limit, this.search)
          this.$message.success(this.title + '成功!')
          this.addVisible = false
        })
    },
    alldeBuild () {
      this.ids = []
      this.debuilds.forEach(builde => {
        this.ids.push(builde.id)
      })
      buildUser.deleteAllBuildUser(this.ids).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getBuilds(this.builds.current, this.limit, this.search)
      })
    },
    changeBox (val) {
      this.debuilds = val
    },
    searchClick () {
      this.getBuilds(1, this.limit, this.search)
    },
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        buildUser.deleteBuildUser(row.id).then(() => {
          this.getBuilds(this.builds.current, this.limit, this.search)
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCurrentChange (page) {
      this.getBuilds(page, this.limit, this.search)
    },
    prevClick () {
      this.getBuilds(this.builds.current - 1, this.limit, this.search)
    },
    nextClick () {
      this.getBuilds(this.builds.current + 1, this.limit, this.search)
    },
    getBuilds (page, limit) {
      buildUser.getBuildUsers(page, limit).then(res => {
        this.builds = res.data
        this.total = parseInt(this.builds.total)
        this.loading = false
      })
    }
  },
  created () {
    this.getBuilds(0, this.limit, this.search)
    user.getUserALl().then(res => {
      this.userIds = res.data
    })
    build.getBuildALls().then(tes => {
      this.buildIds = tes.data
    })
  }
}
</script>

<style scoped>

</style>
